//
//  CZProfileViewController.swift
//  GZWeibo007
//
//  Created by Apple on 15/12/15.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

import SDWebImage

import SVProgressHUD

class CZProfileViewController: CZBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        readCacheSize()
    }
    
    /// 读取缓存大小
    func readCacheSize() {
        SDWebImageManager.sharedManager().imageCache.calculateSizeWithCompletionBlock { (a, b) -> Void in
            print(NSHomeDirectory())
            print("获取到缓存的大小: a: \(a), b: \(b)")
            self.label.text = "缓存大小: \(CGFloat(b) / 1000 / 1000.0) M"
        }
    }
    
    private func prepareUI() {
        view.addSubview(label)
        view.addSubview(button)
        
        label.frame = CGRect(x: 30, y: 100, width: 260, height: 40)
        button.frame = CGRect(x: 30, y: 200, width: 100, height: 40)
    }
    
    func clear() {
        // 在异步线程清除
        SDWebImageManager.sharedManager().imageCache.clearDiskOnCompletion { () -> Void in
            SVProgressHUD.showSuccessWithStatus("清除缓存成功")
            self.readCacheSize()
        }
    }
    
    // MARK: - 懒加载
    // label,显示缓存有多大
    private lazy var label = UILabel()
    
    // 按钮,清除缓存
    private lazy var button: UIButton = {
        let button = UIButton()
        
        button.setTitle("清除缓存", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "clear", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
